﻿using System;
using System.Collections.Generic;
using System.Drawing;
using System.Drawing.Drawing2D;
using System.Linq;
using System.Text;
using System.Windows.Forms;

namespace Marmat.Forms.Skin
{
    /// <summary>
    /// 进度条
    /// </summary>
    public partial class Progress : Control
    {

        /// <summary>
        /// 进度条
        /// </summary>
        public Progress()
        {
            InitializeComponent();
            SetStyle(ControlStyles.SupportsTransparentBackColor, true);
            SetStyle(ControlStyles.UserPaint, true);
            SetStyle(ControlStyles.OptimizedDoubleBuffer, true);
            base.BackColor = Color.Transparent;
        }

        private void InitializeComponent()
        {
            this.SuspendLayout();
            this.ResumeLayout(false);
        }

        protected override void OnPaint(PaintEventArgs e)
        {
            base.OnPaint(e);
            e.Graphics.SmoothingMode = System.Drawing.Drawing2D.SmoothingMode.AntiAlias;
            e.Graphics.InterpolationMode = System.Drawing.Drawing2D.InterpolationMode.HighQualityBicubic;
            var rect = new Rectangle(0, 0, this.Width - 1, this.Height - 1);
            //圆角
            var _circle = this.round ? Math.Min(this.Height, this.Width) / 2 : 0;
            //绘制底色
            using (var bush = new SolidBrush(this.BackColor))
            {
                e.Graphics.FillPath(bush, rect.GetRoundPath(_circle));
                // e.Graphics.SetClip(rect.GetRoundPath(_circle));
            }
            if (this.Percentage > 0)
            {
                using (var bush = new SolidBrush(this.ProgressColor))
                {
                    //进度宽度
                    var strok_width = Convert.ToInt32(this.Width * (this.Percentage / Convert.ToDouble(100)));
                    Rectangle rect_strok_;
                    GraphicsPath path;
                    if (strok_width < this.Height && this.round)
                    {
                        //当进度条为圆角并且宽度小于高度时绘制
                        Bitmap bitmap = new Bitmap(rect.Height, this.Height - 1);
                        using (var graphics = Graphics.FromImage(bitmap))
                        {
                            graphics.SmoothingMode = SmoothingMode.AntiAlias;
                            graphics.InterpolationMode = InterpolationMode.HighQualityBicubic;
                            var rec = new Rectangle(0, 0, this.Height - 2, this.Height - 1);
                            //将圆绘制到Bitmap中
                            graphics.FillPath(bush, rec.GetRoundPath());
                            //绘制左边半圆
                            rect_strok_ = new Rectangle(0, 0, (strok_width / 2), this.Height - 1);
                            e.Graphics.DrawImage(bitmap, 0, 0, rect_strok_, GraphicsUnit.Pixel);
                            //绘制右边半圆
                            rect_strok_ = new Rectangle(this.Height - strok_width / 2, 0, (strok_width / 2), this.Height - 1);
                            e.Graphics.DrawImage(bitmap, strok_width / 2, 0, rect_strok_, GraphicsUnit.Pixel);
                        }
                    }
                    else
                    {
                        rect_strok_ = new Rectangle(0, 0, strok_width, this.Height - 1);
                        e.Graphics.FillPath(bush, rect_strok_.GetRoundPath(_circle));//绘制进度
                    }
                    //绘制进度描述
                    string str = this.Percentage + "%";
                    var font_size = TextRenderer.MeasureText(str, this.Font);
                    if (this.TextInside && font_size.Width <= strok_width)
                    {
                        var font_rec = new Rectangle(strok_width - font_size.Width, (this.Height - font_size.Height) / 2, font_size.Width, font_size.Height);
                        TextRenderer.DrawText(e.Graphics, str, this.Font, font_rec, this.ForeColor, TextFormatFlags.HorizontalCenter | TextFormatFlags.VerticalCenter);
                    }
                }
            }
        }
    }
}
